package com.ztf.shopping.untils;

import com.sun.mail.util.MailSSLSocketFactory;

import javax.mail.*;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.security.GeneralSecurityException;

import java.util.Properties;
import java.util.Random;

/**
 * 发送邮箱验证码
 */
public class SendEmail {

    public static void send(String to,String name){
        // to 收件人电子邮箱

        // 发件人电子邮箱
        String from = "2936408477@qq.com";

        // 指定发送邮件的主机为 smtp.qq.com
        String host = "smtp.qq.com";  //QQ 邮件服务器
        //String host = "smtp.163.com";  //163 邮件服务器

        // 获取系统属性
        Properties properties = System.getProperties();

        // 设置邮件服务器
        properties.setProperty("mail.smtp.host", host);

        //需要验证用户名密码
        properties.put("mail.smtp.auth", "true");

        //String s = randomVerification();

        try{
            //SSL加密
            MailSSLSocketFactory sf = new MailSSLSocketFactory();
            sf.setTrustAllHosts(true);
            properties.put("mail.smtp.ssl.enable", "true");
            properties.put("mail.smtp.ssl.socketFactory", sf);

            // 获取默认session对象
            Session session = Session.getDefaultInstance(properties,new Authenticator(){
                public PasswordAuthentication getPasswordAuthentication(){
                    return new PasswordAuthentication(from, "rxdzshsptngideda"); //发件人邮件用户名、密码
                }
            });


            // 创建默认的 MimeMessage 对象
            MimeMessage message = new MimeMessage(session);

            // 发送人
            message.setFrom(new InternetAddress(from));

            // 添加接受人
            message.addRecipient(Message.RecipientType.TO, new InternetAddress(to));
            //多个接受人  void addRecipients(Message.RecipientType type,Address[] addresses)


            // 标题
            message.setSubject("shopping");

            // 设置普通的文本消息
            //message.setText("");
            //可以设置发送带有HTML内容的消息
            long seconds = System.currentTimeMillis();
            String path = "http://localhost:8080/2_war_exploded/sureRegister?userReg="+name+"&seconds="+seconds;
            message.setContent("<a href="+path+">点击注册</a>","text/html;charset=UTF-8");

            // 发送消息
            Transport.send(message);


        }catch (MessagingException mex) {
            mex.printStackTrace();
        } catch (GeneralSecurityException e) {
            e.printStackTrace();
        }
    }
    public static String send(String to){
        // to 收件人电子邮箱

        // 发件人电子邮箱
        String from = "2936408477@qq.com";

        // 指定发送邮件的主机为 smtp.qq.com
        String host = "smtp.qq.com";  //QQ 邮件服务器
        //String host = "smtp.163.com";  //163 邮件服务器

        // 获取系统属性
        Properties properties = System.getProperties();

        // 设置邮件服务器
        properties.setProperty("mail.smtp.host", host);

        //需要验证用户名密码
        properties.put("mail.smtp.auth", "true");

        //String s = randomVerification();

        try{
            //SSL加密
            MailSSLSocketFactory sf = new MailSSLSocketFactory();
            sf.setTrustAllHosts(true);
            properties.put("mail.smtp.ssl.enable", "true");
            properties.put("mail.smtp.ssl.socketFactory", sf);

            // 获取默认session对象
            Session session = Session.getDefaultInstance(properties,new Authenticator(){
                public PasswordAuthentication getPasswordAuthentication(){
                    return new PasswordAuthentication(from, "rxdzshsptngideda"); //发件人邮件用户名、密码
                }
            });


            // 创建默认的 MimeMessage 对象
            MimeMessage message = new MimeMessage(session);

            // 发送人
            message.setFrom(new InternetAddress(from));

            // 添加接受人
            message.addRecipient(Message.RecipientType.TO, new InternetAddress(to));
            //多个接受人  void addRecipients(Message.RecipientType type,Address[] addresses)


            // 标题
            message.setSubject("shopping");

            // 设置普通的文本消息
            //message.setText("");
            //可以设置发送带有HTML内容的消息
            String s = randomVerification();
            message.setContent("验证码是:"+s+"","text/html;charset=UTF-8");

            // 发送消息
            Transport.send(message);

            return s;
        }catch (MessagingException mex) {
            mex.printStackTrace();
        } catch (GeneralSecurityException e) {
            e.printStackTrace();
        }
        return null;
    }

    private static String randomVerification(){
        String r = "qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM1234567890";
        Random random = new Random();
        StringBuilder result = new StringBuilder();
        for (int i = 0; i < 4; i++) {
            result.append(r.charAt(random.nextInt(62)));
        }
        return result.toString();
    }


}
